#include <stdlib.h>
#include <string.h>
#include "includes/common.h"

void swap(void **arr, int i, int j) {
    void *tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

void arr_move_left(void **arr, int start, int len, int move_len) {
    for (int i = 0; i < len; i++)
    {
        arr[start + i - move_len] = arr[start + i];
    }
}

void arr_move_right(void **arr, int start, int len, int move_len) {
    for (int i = 0; i < len; i++)
    {
        arr[start + len - 1 - i + move_len] = arr[start + len - 1 - i];
    }
}

void arr_move_left_byte(uint8_t *arr, int start, int len, int move_len, int element_size) {
    start *= element_size;
    len *= element_size;
    move_len *= element_size;
    for (int i = 0; i < len; i++)
    {
        arr[start + i - move_len] = arr[start + i];
    }
}

void arr_move_right_byte(uint8_t *arr, int start, int len, int move_len, int element_size) {
    start *= element_size;
    len *= element_size;
    move_len *= element_size;
    for (int i = 0; i < len; i++)
    {
        arr[start + len - 1 - i + move_len] = arr[start + len - 1 - i];
    }
}

int intsort(void *a, void *b) {
    return *((int *) a) - *((int *) b);
}

char *str_concat(char *a, char *b) {
    int alen = strlen(a);
    int blen = strlen(b);
    char *res = malloc(alen + blen + 1);
    memcpy(res, a, alen);
    memcpy(res + alen, b, blen + 1);
    return res;
}
